package com.buer.storage.service.impl;

import com.buer.storage.dao.*;
import com.buer.storage.dto.*;
import com.buer.storage.entity.*;
import com.buer.storage.service.WorderService;
import com.buer.storage.utils.CheckEmptyUtil;
import com.buer.storage.utils.CommonGeneratorUtil;
import com.buer.storage.utils.DateUtils;
import com.buer.storage.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WorderServiceImpl implements WorderService {
    private SnowFlake snowFlake = new SnowFlake(2, 3);
    @Resource
    private WorderDao worderDao;

    @Autowired
    private ProdDao prodDao;

    @Autowired
    private WorderdetailsDao worderdetailsDao;

    @Autowired
    private StockDao stockDao;

    @Autowired
    private AreaDao areaDao;

    @Autowired
    private StockdetailsDao stockdetailsDao;



    //增加入库信息
    @Transactional
    @Override
    public int insert(PutStorageDto putStorageDto) {
        Map prodMap =new HashMap<>();
        String orderNo = String.valueOf(snowFlake.nextId());
        putStorageDto.setOrderNum("PS" + orderNo);
//        putStorageDto.setPutStatus(0);
        int result = worderDao.insertOrder(putStorageDto);
        for (int i = 0; i < putStorageDto.getPutProds().length; i++) {
            Worderdetails putProd = putStorageDto.getPutProds()[i];
            putProd.setWorderPutid(putStorageDto.getPutId());
            //判断是否有装备
            Prod one =null;
           if( CheckEmptyUtil.isEmpty(putProd.getProdCode())){
               one=new Prod();
               one.setProdName(putProd.getProdName());
               one.setProdCode(getProdCode());
               one.setProdSpecial(putProd.getProdSpecial());
               one.setEmit(putProd.getEmit());
               one.setSupplierId(putProd.getSupplierId());
               one.setCategoryTwoId(putProd.getCategoryTwoId());
               prodDao.save(one);
           }else {
               prodMap.put("prodCode",putProd.getProdCode());
                one = prodDao.getOne(prodMap);
           }

            putProd.setProdImg(one.getProdImg());
            putProd.setProdCode(one.getProdCode());
        }
        worderDao.insertDetails(putStorageDto.getPutProds());
        return result;
    }

    public String getProdCode() {
        String prodCode = CommonGeneratorUtil.generatorProdCode();
        ProdDto prodByCode = prodDao.getProdByCode(prodCode);
        if (prodByCode != null) {
            return getProdCode();
        } else {
            return prodCode;
        }

    }

    @Override
    public int save(PutStorageDto putStorageDto) {
        return worderDao.insertOrder(putStorageDto);
    }

    //查询所有订单表信息
    @Override
    public List<WorderDto> findOrderAll() {
        List<WorderDto> orderAll = worderDao.findOrderAll();
        return orderAll;
    }

    @Override
    public PageInfo<Worder> selectByAll(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Worder> list = worderDao.selectByAll(map); // 获取数据
        PageInfo<Worder> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }


    //根据订单表id删除入库信息
    @Transactional
    @Override
    public int deletePutStorageById(int putId) {
        int deleteWorderById = worderDao.deletePutStorageById(putId);
        return deleteWorderById;
    }


    //根据商品id删除商品信息
    @Override
    public int deleteWorderdetails(int detailsId) {
        int deleteWorderdetails = worderDao.deleteWorderdetails(detailsId);
        return deleteWorderdetails;
    }

    //更新商品id
    @Override
    @Transactional
    public int updateWorderdetails(PutStorageDto putStorageDto) {
        // 第一步先删除,此处需要前段讲putId的值传入过来
        Integer putId = putStorageDto.getPutId();
        worderDao.deleteWorderByPutId(putId);
        worderDao.deleteWorderDetailsByPutId(putId);
        // 第二部再插入
        //putStorageDto.setPutStatus(1);
        int result = worderDao.insertOrder(putStorageDto);
        for (int i = 0; i < putStorageDto.getPutProds().length; i++) {
            Worderdetails putProd = putStorageDto.getPutProds()[i];
            putProd.setWorderPutid(putStorageDto.getPutId());
        }
        worderDao.insertDetails(putStorageDto.getPutProds());
        return result;
    }


    //根据订单表id查询入库信息
    @Override
    public List<PutStorageAllDto> findPutAll(Integer putId) {
        List<PutStorageAllDto> all = worderDao.findPutAll(putId);
        if (all.size() > 0) {
            for (int i = 0; i < all.size(); i++) {
                WorderdetailsDto[] info = worderDao.findOrderDetailInfoByPutId(all.get(i).getPutId());
                if (info.length > 0) {
                    all.get(i).setPutProds(info);
                }
            }

        }
        return all;
    }

    //入库单表增加
    @Override
    @Transactional
    public int submitAddBuerStock(Integer putId) {
        Map map=new HashMap<>();
        Map stockMap=new HashMap<>();

        map.put("worderPutid",putId);
        List<WorderdetailsDO> worderdetailsList = worderdetailsDao.getWorderdetailsList(map);
        for (WorderdetailsDO worderdetailsDO:worderdetailsList)  {
            stockMap.put("prodCode",worderdetailsDO.getProdCode());
            stockMap.put("prodArea",worderdetailsDO.getProdArea());
            stockMap.put("prodDepart",worderdetailsDO.getProdDepart());
            Stock one = stockDao.getOne(stockMap);
            if(one == null){
                //如果stock表里没有的话就插入
                one=new Stock();
                one.setProdImg(worderdetailsDO.getProdImg());
                one.setProdCode(worderdetailsDO.getProdCode());
                one.setProdName(worderdetailsDO.getProdName());
                one.setProdSpecial(worderdetailsDO.getProdSpecial());
                one.setProdStocknum(worderdetailsDO.getProdNum());
                one.setProdAwaitnum(0);
                one.setProdResiduenum(worderdetailsDO.getProdNum());
                one.setEmit(worderdetailsDO.getEmit());
                one.setProdArea(worderdetailsDO.getProdArea());
                one.setProdDepart(worderdetailsDO.getProdDepart());
                Area area = areaDao.selectById(worderdetailsDO.getProdArea());
                one.setWarehouseId(area.getWarehouseId());
                one.setWorderPutid(putId);
                one.setProdDate(worderdetailsDO.getProdDate());
                one.setCategoryTwoId(worderdetailsDO.getCategoryTwoId());
                stockDao.save(one);
            }else {
                one.setProdStocknum(one.getProdStocknum()+worderdetailsDO.getProdNum());
                one.setProdResiduenum(worderdetailsDO.getProdNum()+worderdetailsDO.getProdNum());
                stockDao.update(one);
            }
            //循环插入库存详情表
            for (int i=0;i<worderdetailsDO.getProdNum();i++){
                Stockdetails stockdetails=new Stockdetails();
                stockdetails.setRfid(System.currentTimeMillis()+"");
                stockdetails.setProdCode(worderdetailsDO.getProdCode());
                stockdetails.setProdName(worderdetailsDO.getProdName());
                stockdetails.setProdSpecial(worderdetailsDO.getProdSpecial());
                stockdetails.setProdNum(1);
                stockdetails.setEmit(worderdetailsDO.getEmit());
                stockdetails.setProdArea(worderdetailsDO.getProdArea());
                stockdetails.setProdDepart(worderdetailsDO.getProdDepart());
                stockdetails.setProdStatus(0);
                stockdetails.setCategoryTwoId(worderdetailsDO.getCategoryTwoId());
                stockdetailsDao.save(stockdetails);
            }


        }

        //更新入库订单表
        Worder worder = worderDao.get(putId);
        worder.setPutDate(DateUtils.getDate());
        worder.setPutStatus(1);
        worderDao.update(worder);

        return 1;
    }

    @Override
    public PageInfo<WorderDto> getWord(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<WorderDto> list = worderDao.getWord(map); // 获取数据
        PageInfo<WorderDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<WorderdetailsDto> getWorderdetails(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<WorderdetailsDto> list = worderDao.getWorderdetails(map); // 获取数据
        PageInfo<WorderdetailsDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<StockDto> getStock(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<StockDto> list = worderDao.getStock(map); // 获取数据
        PageInfo<StockDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }
}
